package com.shareach.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.mongo.BasePlaceDao;
import com.shareach.domain.BasePlaceDomain;
import com.shareach.service.BasePlaceService;
import com.shareach.service.exception.BaseService;
import com.shareach.util.ReflectionUtil;

/**
 * create time：2013-1-30 下午6:26:32
 * 
 * @author retryu
 * @since JDK 1.6
 * @version 1.0 description：
 */
@Service
public class BasePlaceServiceImpl extends BaseService<BasePlaceDomain> implements BasePlaceService {

	@Autowired
	private BasePlaceDao basePlaceDao;

	@Override
	public Long save(BasePlaceDomain domain) {
		return basePlaceDao.insert(domain);
	}

	@Override
	public void del(BasePlaceDomain domain) {
		if (domain.getId() == null)
			return;
		Query query = new Query();
		query.addCriteria(new Criteria("_id").is(domain.getId()));
		basePlaceDao.delete(query, BasePlaceDomain.class);

	}

	@Override
	public void update(BasePlaceDomain domain) {

	}

	@Override
	public BasePlaceDomain getById(Long id) {
		if (id == null)
			return null;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		BasePlaceDomain basePlaceDomain = basePlaceDao.findOne(query);
		return basePlaceDomain;
	}

	@Override
	public List<BasePlaceDomain> getList(BasePlaceDomain domain) {
		Query query = new Query();
		Map<Object, Object> domainMap = ReflectionUtil.parseDomaintoMap(domain);
		for (Map.Entry<Object, Object> entry : domainMap.entrySet()) {
			query.addCriteria(Criteria.where(entry.getKey().toString()).is(entry.getValue()));
		}
		List<BasePlaceDomain> baseDomains = basePlaceDao.findList(query);
		return baseDomains;
	}

	@Override
	public Page<BasePlaceDomain> getPageResult(BasePlaceDomain domain, BaseQueryOrder order) {
		Query query = parseDomaintoQuery(domain);
		Page<BasePlaceDomain> domainPage = basePlaceDao.getPageResult(query, order);
		return domainPage;
	}

}
